var carthepler = require("../../js/carthepler.js").carthepler, app = getApp();

Page({
    data: {
        carts: [],
        limitcarsdata: [],
        hasList: "",
        totalPrice: 0,
        selectAllStatus: !1,
        totalNum: 0,
        intervalid: -1,
        edit_hidden: !1
    },
    cartselct: function(t, a, e, i) {
        var r = "";
        r = 2 == i ? "limitcars" : "cars";
        var s = wx.getStorageSync(r) || [];
        if (1 == t) s[a].selected = e; else for (var c = 0, n = s.length; c < n; c++) s[c].selected = e;
        wx.setStorage({
            key: r,
            data: s
        });
    },
    loadcartdata: function() {
        carthepler.loadcartdata(this);
    },
    onShow: function() {
        this.loadcartdata(), this.autoexcel();
    },
    onUnload: function() {
        this.clearauto();
    },
    onHide: function() {
        this.clearauto();
    },
    edit: function() {
        this.setData({
            edit_hidden: !0
        });
    },
    edit_no: function() {
        this.setData({
            edit_hidden: !1
        });
    },
    onLoad: function() {
        app.look.footer(this), app.look.navbar(this);
    },
    onGotUserInfo: function(t) {
        app.look.getuserinfo(t, this);
    },
    selectList: function(t) {
        var a = t.currentTarget.dataset, e = a.index, i = a.op, r = [], s = "carts";
        2 == i ? (r = this.data.limitcarsdata, s = "limitcarsdata") : r = this.data.carts, 
        r[e].selected = !r[e].selected, this.cartselct(1, e, r[e].selected, i);
        var c = {};
        c[s] = r, this.setData(c), carthepler.getTotalPrice(this);
    },
    clearcart: function(t) {
        this.setData({
            hasList: !1
        });
    },
    selectAll: function(t) {
        var a = this.data.selectAllStatus;
        a = !a;
        var e = this.data.carts;
        this.cartselct(2, -1, a, 1), this.cartselct(2, -1, a, 2);
        for (var i = 0; i < e.length; i++) e[i].selected = a;
        for (var r = this.data.limitcarsdata, s = 0; s < r.length; s++) r[s].selected = a;
        this.setData({
            selectAllStatus: a,
            limitcarsdata: r,
            carts: e
        });
        carthepler.getTotalPrice(this);
    },
    numchage: function(t) {
        var a = t.currentTarget.dataset, e = a.index, i = a.op, r = parseInt(a.val);
        if (2 == i) {
            var s = this.data.limitcarsdata, c = s[e].flash_id, n = s[e].id, l = c + "_" + n;
            if (0 < r) {
                var o = 0;
                if (s.forEach(function(t) {
                    t.flash_id == c && t.id == n && (o += parseInt(t.num));
                }), !(o < parseInt(carthepler.temdata[l].userlimit))) return;
                s[e].num += r;
            } else s[e].num += r;
            var d = wx.getStorageSync("limitcars") || [];
            s[e].num < 1 ? (s.splice(e, 1), d.splice(e, 1)) : d[e].num = s[e].num, wx.setStorage({
                key: "limitcars",
                data: d
            }), this.setData({
                limitcarsdata: s
            });
        } else {
            var u = this.data.carts;
            if (u[e].num = parseInt(u[e].num) + r, u[e].num > u[e].stock) return;
            var h = wx.getStorageSync("cars") || [];
            u[e].num < 1 ? (u.splice(e, 1), h.splice(e, 1)) : h[e].num = u[e].num, wx.setStorage({
                key: "cars",
                data: h
            }), this.setData({
                carts: u
            });
        }
        carthepler.getTotalPrice(this);
    },
    addCount: function(t) {
        var a = t.currentTarget.dataset.index, e = (dataset.op, this.data.carts);
        if (e[a].num += 1, !(e[a].num > e[a].stock)) {
            var i = wx.getStorageSync("cars") || [];
            i[a].num = e[a].num, wx.setStorage({
                key: "cars",
                data: i
            }), this.setData({
                carts: e
            }), carthepler.getTotalPrice(this);
        }
    },
    minusCount: function(t) {
        var a = t.currentTarget.dataset.index, e = this.data.carts;
        if (1 != e[a].num) {
            e[a].num -= 1, e[a].num < 1 && e.splice(a, 1);
            var i = wx.getStorageSync("cars") || [];
            i[a].num = e[a].num, wx.setStorage({
                key: "cars",
                data: i
            }), this.setData({
                carts: e
            }), carthepler.getTotalPrice(this);
        }
    },
    Settlement: function() {
        0 < this.data.limitcarsdata.length ? app.globalData.temdata = this.data.limitcarsdata.filter(function(t) {
            return t.selected;
        }) : app.globalData.temdata = [];
        var t = this.data.carts, a = this.data.totalPrice, e = this.data.totalNum, i = [], r = [];
        t.forEach(function(t, a) {
            if (!0 === t.selected) {
                var e = {
                    id: t.id,
                    img: t.bimg,
                    num: t.num,
                    price: t.price,
                    name: t.name,
                    attr: t.attr,
                    weight: t.weight
                };
                1 == app.globalData.webset.vip && app.globalData.userInfo.member && (e.prodiscount = .001 <= t.vprice ? t.vprice : app.globalData.userInfo.member.discount), 
                i.push(e);
            }
        }), i.length < 1 && app.globalData.temdata.length < 1 || (r = {
            content: i,
            totalPrice: a,
            totalNum: e,
            cid: 1
        }, r = JSON.stringify(r), r = encodeURIComponent(r), wx.navigateTo({
            url: "../submit/submit?order=" + r
        }));
    },
    Deltlement: function() {
        var e = this.data.carts, i = [], r = [];
        e.forEach(function(t, a) {
            1 == t.selected || (i.push(e[a]), r.push({
                id: t.id,
                num: t.num,
                price: t.price,
                attr: t.attr,
                cid: t.cid
            }));
        }), wx.setStorage({
            key: "cars",
            data: r,
            success: function() {}
        });
        for (var t = wx.getStorageSync("limitcars") || [], a = this.data.limitcarsdata, s = a.length - 1; 0 <= s; s--) a[s].selected && (a.splice(s, 1), 
        t.splice(s, 1));
        wx.setStorage({
            key: "limitcars",
            data: t,
            success: function() {}
        }), this.setData({
            carts: i,
            limitcarsdata: a,
            edit_hidden: !1
        });
    },
    onReady: function() {
        app.look.navbar(this);
    },
    clearauto: function() {
        -1 != this.data.intervalid && (clearInterval(this.data.intervalid), this.data.intervalid = -1);
    },
    autodealfunc: function() {
        var t = Date.parse(new Date());
        t /= 1e3;
        var a = this.data.limitcarsdata;
        if (a && 0 < a.length) {
            for (var e = a.length - 1; 0 <= e; e--) t > a[e].expires && a.splice(e, 1);
            0 == a.length && this.clearauto(), this.setData({
                limitcarsdata: a
            });
        } else this.clearauto();
    },
    autoexcel: function() {
        var t = this;
        -1 != t.data.intervalid && t.clearauto(), t.data.intervalid = setInterval(function() {
            t.autodealfunc();
        }, 1e3);
    },
    onPullDownRefresh: function() {
        this.loadcartdata();
    },
    clearStorage: function() {
        app.globalData.temdata = [];
        try {
            wx.clearStorageSync(), wx.showToast({
                title: "清除缓存成功"
            }), app.look.footer(this);
        } catch (t) {}
    }
});